package com.zywl.utils.utils;

import com.zywl.core.beans.Association;
import com.zywl.service.AssociationService;
import com.zywl.service.DictionaryService;
import com.zywl.service.LogService;
import com.zywl.utils.utils.enums.LogTypeEnum;
import com.zywl.utils.utils.enums.RoleLevelEnum;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class AsyncMethodUtil {
    protected static final Logger logger = Logger.getLogger(AsyncMethodUtil.class);
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private LogService iLogService;
    @Autowired
    private AssociationService associationService;

    /**
     * 获取字典json到session
     *
     * @param session
     */
    @Async
    public void putDictToJson(HttpSession session) {
        try {
            String json = JsonUtils.objectToJson(dictionaryService.getListAll("dis_type, dis_sort"));
            session.setAttribute(Constants.DICTIONARY_SESSION_KEY, json);
        } catch (Exception e) {
            logger.error("获取字典项异常", e);
        }
    }


    /**
     * 获取枚举到session
     *
     * @param session
     */
    @Async
    public void putEnumsDataToJson(HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleLevel", RoleLevelEnum.getListData());
        map.put("logType", LogTypeEnum.getListData());
        session.setAttribute(Constants.ENUM_SESSION_KEY, JsonUtils.objectToJson(map));
    }

    /**
     * 获取协会枚举到session
     *
     * @param session
     */
    @Async
    public void putAssociationToJson(HttpSession session,Long associationId) {
        try {
            Map<String,Object> map = new HashMap<>();
            List<Association> associationList = associationService.getListByParams(map);
            List<Association> list = new ArrayList<>();
            for (Association association : associationList){
                Association a = new Association();
                a.setAssId(association.getAssId());
                a.setAssName(association.getAssName());
                list.add(a);
            }
            String json = JsonUtils.objectToJson(list);
            session.setAttribute(Constants.XIEHUI_SESSION_KEY, json);
        } catch (Exception e) {
            logger.error("获取协会列表异常", e);
        }
    }
}
